package com.thunder.kocho.loki;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Rect;

import com.thunder.kocho.loki.builder.KeyFrameAnimatorBuilder;
import com.thunder.kocho.loki.shape.RectLoki;

import java.util.ArrayList;

/**
 * @date 2021-2-22
 */
public class RectGrid extends LokiGroup {

    @Override
    protected ArrayList<Loki> onCreateChild() {
        int[] delayArray =new int[]{200,300,400,100,200,300,0,100,200};
        ArrayList<Loki> lokis=new ArrayList<>();
        for(int i=0;i<9;i++){
            RectGirdLoki rectGirdLoki= new RectGirdLoki();
            rectGirdLoki.setAnimationDelays(delayArray[i]);
            lokis.add(rectGirdLoki);
        }
        return lokis;
    }

    @Override
    protected void onChildCreated(ArrayList<Loki> lokis) {

    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        bounds=clipSquare(bounds);
        float width=bounds.width()/3f;
        float height=bounds.height()/3f;
        for(int i=0;i<9;i++){
            int x=i%3;
            int y=i/3;
            float left=bounds.left+x*width;
            float top=bounds.top+y*height;
            Loki loki=getChildAt(i);
            loki.setLokiBoundsAndPivot(left,top,left+width,top+height);
        }
    }

    private static class RectGirdLoki extends RectLoki {
        @Override
        protected Animator onCreateAnimator() {
            return new KeyFrameAnimatorBuilder()
                    .scale(new float[]{0f,0.35f,0.7f,1f},1f,0f,1f,1f)
                    .count(ValueAnimator.INFINITE)
                    .target(this)
                    .duration(1300)
                    .build();
        }
    }

}
